// Server duoc yeu cau chuyen khoan
	public void Chuyenkhoan(String tkgui, String tknhan, int sotien)
			throws Exception {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		String day = sdf.format(cal.getTime());
		int stiennguoigui = 0;
		int stiennguoinhan = 0;
		int success = 0;
		String url = "com.mysql.jdbc.Driver";
		synchronized (Getaccount(tkgui)) {

			try {
				Class.forName(url);
				Connection cn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/atm", "root", "");
				String sql2 = "select* from khachhang where sotaikhoan='"
						+ tknhan + "'";
				PreparedStatement stmt1 = cn.prepareStatement(sql2);
				ResultSet rs2 = stmt1.executeQuery();
				while (rs2.next()) {
					stiennguoinhan = rs2.getInt("tongtien");
				}
				String sql = "select* from khachhang where sotaikhoan='"
						+ tkgui + "'";
				PreparedStatement stmt = cn.prepareStatement(sql);
				ResultSet rs = stmt.executeQuery();
				while (rs.next()) {
					stiennguoigui = rs.getInt("tongtien");
				}
				if (stiennguoigui - sotien >= 50000) {
					stiennguoigui = stiennguoigui - sotien;
					String sql3 = "update khachhang set tongtien="
							+ stiennguoigui + " where sotaikhoan='" + tkgui
							+ "'";
					PreparedStatement stmt3 = cn.prepareStatement(sql3);
					stmt3.executeUpdate();
					stiennguoinhan = stiennguoinhan + sotien;
					String sql4 = "update khachhang set tongtien="
							+ stiennguoinhan + " where sotaikhoan='" + tknhan
							+ "'";
					PreparedStatement stmt4 = cn.prepareStatement(sql4);
					stmt4.executeUpdate();
					// ghi lich su giao dich
					String sql5 = "insert into lichsugiaodich(sotaikhoan,ngaythang,sotien,lydo) Values('"
							+ tkgui
							+ "',"
							+ day
							+ ","
							+ sotien
							+ ",'ChuyenTien')";
					PreparedStatement stmt5 = cn.prepareStatement(sql5);
					stmt5.executeUpdate();
					String sql6 = "insert into lichsugiaodich(sotaikhoan,ngaythang,sotien,lydo) Values('"
							+ tknhan
							+ "',"
							+ day
							+ ","
							+ sotien
							+ ",'NhanChuyenTien')";
					PreparedStatement stmt6 = cn.prepareStatement(sql6);
					stmt6.executeUpdate();
					System.out.println("Da ghi lai lich su giao dich");
					success = 1;
				}

				cn.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			out.println(success);
			out.flush();
			out.println(stiennguoigui);
			out.flush();
		}

	}

	// Server duoc yeu cau kiem tra tai khoan
	public void Kiemtra(String stk) throws Exception {
		int sodong = 0;
		String url = "com.mysql.jdbc.Driver";
		try {
			Class.forName(url);
			Connection cn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/atm", "root", "");
			String sql = "select* from khachhang where sotaikhoan='" + stk
					+ "'";
			PreparedStatement stmt1 = cn.prepareStatement(sql);
			ResultSet rs = stmt1.executeQuery();
			while (rs.next()) {
				int stiennguoinhan = rs.getInt("tongtien");
				sodong++;
			}
			cn.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		out.println(sodong);
		out.flush();
	}
}

public class Server {
	public static void main(String[] args) {		
		try {
			ServerSocket serversocket = new ServerSocket(8000);// tao socket
																// chap nhan ket
																// noi tu client
			int clientno = 1; // so may client
			while (true) {

				Socket connecttoclient = serversocket.accept();// chap nhan ket
				System.out.println("Khoi dong cho luong client so: "+clientno);
				// xac dinh dia chi
				InetAddress clientip = connecttoclient.getInetAddress(); // lay
																			// dia
																			// chi
																			// cua
																			// may client
				
				System.out.println("Host name cua client " + clientno + " la "
						+ clientip.getHostName());
				System.out.println("Dia chi IP cua client " + clientno + " la "
						+ clientip.getHostAddress()+"\n");
				SQLServer myobject = new SQLServer(connecttoclient);// ket noi
																	// den csdl
				Thread thserver = new Thread(myobject);// cho phep viec truy cap
														// csdl xay ra dong thoi
														// vs viec ket noi den
														// client
				thserver.start();
				clientno++; // tang so may client len 1

			}
		} catch (IOException e) {
			System.out.println("Server khong the ket noi....hay thu lai");
		}
	}
}
